package logic

import (
	"ViewSrv/global/model"
	"context"

	"ViewSrv/internal/svc"
	"ViewSrv/view"

	"github.com/zeromicro/go-zero/core/logx"
)

type ViewLogLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewViewLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ViewLogLogic {
	return &ViewLogLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ViewLogLogic) ViewLog(in *view.ViewLogRequest) (*view.ViewLogResponse, error) {
	// todo: add your logic here and delete this line
	var Videos []struct {
		model.Video
		LostOn int64
	}
	sql := model.DB.Model(&model.VideoLog{}).Joins("join videos on videos.id = video_logs.video_id join video_log_lens on video_log_lens.video_id = videos.id").Select("videos.*,video_log_lens.len as lost_on").Where("video_logs.user_id = ?", in.UserId)
	if in.Video != "" {
		sql = sql.Where("videos.title like ?", "%"+in.Video+"%")
	}
	if err := sql.Find(&Videos).Error; err != nil {
		return nil, err
	}
	var Data []*view.FormLogVideo
	for _, i2 := range Videos {
		var data = &view.FormLogVideo{
			Title:   i2.Video.Title,
			LostLen: i2.LostOn,
		}
		Data = append(Data, data)
	}
	return &view.ViewLogResponse{
		Videos: Data,
	}, nil
}
